//
//  FaceDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/5/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "FaceDetailViewController.h"
#import "LineDetailDescTableViewCell.h"
#import "SelectPhotoTableViewCell.h"
#import "ButtonTableViewCell.h"
#import "ImageShowTableViewCell.h"
#import "ESPictureBrowser.h"
#import "CustomerFaceGatherViewController.h"
#import "FaceGatherViewController.h"

@interface FaceDetailViewController () <ESPictureBrowserDelegate>
@property (nonatomic, strong) ESPictureBrowser *browser;
@property (nonatomic, strong) NSMutableArray *photoArray;
@end

@implementation FaceDetailViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (![self.userModel isKindOfClass:[SelectUserInfoModel class]]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            Show_SV_Error_Msg(@"数据错误")
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"人脸详情"];
    self.tableView.mj_header = nil;
    self.tableView.mj_footer = nil;
    
    self.photoArray = [NSMutableArray array];
    if ([self.userModel.face_img1 isNotEmptyString]) {
        [self.photoArray addObject:self.userModel.face_img1];
    }
    if ([self.userModel.face_img2 isNotEmptyString]) {
        [self.photoArray addObject:self.userModel.face_img2];
    }
    if ([self.userModel.face_img3 isNotEmptyString]) {
        [self.photoArray addObject:self.userModel.face_img3];
    }
    
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.titleLabel.text = @"采集用户";
        cell.contentLabel.text = self.userModel.type == 0 ? (self.userModel.username?:@"") : (self.userModel.visitor_name?:@"");
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 1) {
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.titleLabel.text = self.userModel.type == 1 ? @"身份证号码":@"手机号码";
        cell.contentLabel.text = self.userModel.type == 1 ? (self.userModel.user_account?:@"") : (self.userModel.mobile?:@"");
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.row == 2) {
        ImageShowTableViewCell *cell = [ImageShowTableViewCell cellWithTableView:tableView class:[ImageShowTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = @"人脸照片";
        if (self.photoArray.count>0) {
            [cell.image1 sd_setImageWithURL:[NSURL URLWithString:[self.photoArray objectAtIndex:0]] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
            cell.image1.hidden = NO;
        }
        else{
            cell.image1.hidden = YES;
        }
        if (self.photoArray.count>1) {
            [cell.image2 sd_setImageWithURL:[NSURL URLWithString:[self.photoArray objectAtIndex:1]] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
            cell.image2.hidden = NO;
        }
        else{
            cell.image2.hidden = YES;
        }
        if (self.photoArray.count>2) {
            [cell.image3 sd_setImageWithURL:[NSURL URLWithString:[self.photoArray objectAtIndex:2]] placeholderImage:ImageNamed_bb(@"home_list_default_img")];
            cell.image3.hidden = NO;
        }
        else{
            cell.image3.hidden = YES;
        }
        
        BMWeakSelf
        [cell setBlockClickImage:^(NSInteger index) {
            weakSelf.browser = [[ESPictureBrowser alloc] initWithTitles:nil contents:nil];
            weakSelf.browser.delegate = weakSelf;
            [weakSelf.browser showFromView:weakSelf.view picturesCount:weakSelf.photoArray.count currentPictureIndex:index];
        }];
        
        return cell;
    }
    if (indexPath.row == 3) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = @"重新采集";
        cell.isDelete = NO;
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [weakSelf goToFaceGather];
        }];
        return cell;
    }
    if (indexPath.row == 4) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = @"删除";
        cell.isDelete = YES;
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [UIAlertView bk_showAlertViewWithTitle:@"是否删除人脸照片" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"删除"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    [weakSelf requestDeleteFaceGather];
                }
            }];
        }];
        
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 2) {
        return 50.f;
    }
    if (indexPath.row == 2) {
        return 160.f;
    }
    if (indexPath.row == 3) {
        return 90.f;
    }
    if (indexPath.row == 4) {
        return 90.f;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)requestDeleteFaceGather {
    Show_Msg(@"删除中")
    BMWeakSelf
    [HttpRequest deleteFaceDataWithSeqid:self.userModel.seqid success:^(id object) {
        Show_SV_Success_Msg(@"删除成功")
        if (weakSelf.BlockDeleteSuccess) {
            weakSelf.BlockDeleteSuccess();
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *code, NSString *msg) {
        Show_SV_Error_Msg(msg)
    }];
}

- (void)goToFaceGather {
    BMWeakSelf
    if (self.userModel.type == 1) {
        //访客
        CustomerFaceGatherViewController *vc = [CustomerFaceGatherViewController new];
        vc.userModel = self.userModel;
        vc.isFromRegather = YES;
        [vc setBlockGatherCustomerSuccess:^{
            if (weakSelf.blockRegatherSuccess) {
                weakSelf.blockRegatherSuccess();
            }
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        //用户
        FaceGatherViewController *vc = [FaceGatherViewController new];
        vc.userModel = self.userModel;
        vc.isFromRegather = YES;
        [vc setBlockGatherFaceSuccess:^{
            if (weakSelf.blockRegatherSuccess) {
                weakSelf.blockRegatherSuccess();
            }
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (NSString *)pictureView:(ESPictureBrowser *)pictureBrowser highQualityUrlStringForIndex:(NSInteger)index {
    return [self.photoArray objectAtIndexSafe:index];
}
@end
